<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="营运证号" prop="licenseNum">
        <el-input
          v-model="queryParams.licenseNum"
          placeholder="请输入营运证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"

          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vehicleservice:vehicleinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"

          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['vehicleservice:vehicleinfo:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"

          icon="el-icon-upload2"
          size="mini"
          @click="openImport"
          v-hasPermi="['vehicleservice:vehicleinfo:import']"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="vehicleinfoList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="营运证号" align="center" prop="licenseNum" />
      <el-table-column label="车牌号" align="center" prop="brand" />
      <el-table-column label="车牌颜色" align="center" prop="brandColor" />
      <el-table-column label="品牌型号" align="center" prop="model" />
      <el-table-column label="营运性质" align="center" prop="operationalNature" />
      <el-table-column label="燃料类型" align="center" prop="fuelType" />
      <el-table-column label="车辆类型" align="center" prop="vehicleType" />
      <el-table-column label="车架号" align="center" prop="vin" />
      <el-table-column label="发动机号" align="center" prop="engineNum" />
      <el-table-column label="核定载人数" align="center" prop="seatNum" />
      <el-table-column label="整备质量" align="center" prop="weight" />
      <el-table-column label="初次注册日期 " align="center" prop="registerDate" />
      <el-table-column label="托修方" align="center" prop="txUnit" />
      <el-table-column label="托修方电话" align="center" prop="txUnitTel" />
      <el-table-column label="维护周期（单位 月）" align="center" prop="whcycle" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['vehicleservice:vehicleinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['vehicleservice:vehicleinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--导入-->
    <el-dialog
      title="提示"
      :visible.sync="openImportData"
      width="30%"
      :before-close="handleClose">
      <el-upload
        class="upload-demo"
        :action="importUrl"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        :on-success="uploadSuccess"
        :limit="1"
        :accept="accept"
        :headers="headers"
        :on-exceed="handleExceed"
        :file-list="fileList">
        <el-button size="small" type="primary">点击上传</el-button>
        <div slot="tip" class="el-upload__tip">只能上传.xls /.xlsx文件</div>
      </el-upload>
      <span slot="footer" class="dialog-footer">
        <el-button @click="openImportData = false">取 消</el-button>
        <el-button type="primary" @click="openImportData = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加或修改汽车检测-车辆信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body :before-close="dilogClose">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="营运证号" prop="licenseNum">
          <el-input v-model="form.licenseNum" maxlength="50" placeholder="请输入营运证号" />
        </el-form-item>
        <el-form-item label="车牌号" prop="brand">
          <el-input v-model="form.brand" maxlength="30" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="车牌颜色" prop="brandColor">
          <el-input v-model="form.brandColor" maxlength="10" placeholder="请输入车牌颜色" />
        </el-form-item>
        <el-form-item label="品牌型号" prop="model">
          <el-input v-model="form.model" maxlength="10" placeholder="请输入品牌型号" />
        </el-form-item>
        <el-form-item label="营运性质" prop="operationalNature">
          <el-input v-model="form.operationalNature" maxlength="10" placeholder="请输入营运性质" />
        </el-form-item>
        <el-form-item label="车架号" prop="vin">
          <el-input v-model="form.vin" maxlength="50" placeholder="请输入车架号" />
        </el-form-item>
        <el-form-item label="发动机号" prop="engineNum">
          <el-input v-model="form.engineNum" maxlength="50" placeholder="请输入发动机号" />
        </el-form-item>
        <el-form-item label="核定载人数" prop="seatNum">
          <el-input v-model="form.seatNum" placeholder="请输入核定载人数" />
        </el-form-item>
        <el-form-item label="整备质量" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入整备质量" />
        </el-form-item>
        <el-form-item label="车辆类型" prop="vehicleType">
          <el-input v-model="form.vehicleType" maxlength="50" placeholder="请输入车辆类型" />
        </el-form-item>
        <el-form-item label="燃油类型" prop="fuelType">
          <el-input v-model="form.fuelType" maxlength="10" placeholder="请输入燃油类型" />
        </el-form-item>
        <el-form-item label="初次注册日期 " prop="registerDate">
          <el-date-picker style="width: 100%;" v-model="form.registerDate" type="date" placeholder="选择日期" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd"></el-date-picker>
        </el-form-item>
        <el-form-item label="托修方" prop="txUnit">
          <el-input v-model="form.txUnit" placeholder="请输入托修方" />
        </el-form-item>
        <el-form-item label="托修方电话" prop="txUnitTel">
          <el-input v-model="form.txUnitTel" maxlength="20" placeholder="请输入托修方电话" />
        </el-form-item>
        <el-form-item label="维护周期" prop="whcycle">
          <el-input v-model="form.whcycle" maxlength="30" placeholder="请输入维护周期" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVehicleinfo, getVehicleinfo, delVehicleinfo, addVehicleinfo, updateVehicleinfo } from "@/api/maintenanceenterprise/vehicleinfo";
import { getToken } from "@/utils/auth";
export default {
  name: "Vehicleinfo",
  data() {
    return {
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      fileList: [],
      accept: ".xls,.xlsx",
      importUrl: process.env.VUE_APP_BASE_API + "/vehicleservice/vehicleinfo/import/data",
      openImportData: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 汽车检测-车辆信息表格数据
      vehicleinfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        licenseNum: null,
        brand: null,
        brandColor: null,
        fuelType: null,
        vehicleType: null,
        vin: null,
        engineNum: null,
        seatNum: null,
        weight: null,
        registerDate: null,
        txUnit: null,
        txUnitTel: null,
        whcycle: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        brand: [
          { required: true, message: '请输入车牌号', trigger: 'blur' },
        ],
        brandColor: [
          { required: true, message: '请输入车牌颜色', trigger: 'blur' },
        ],
        model: [
          { required: true, message: '请输入品牌型号', trigger: 'blur' },
        ],
        operationalNature: [
          { required: true, message: '请输入营运性质', trigger: 'blur' },
        ],
        vin: [
          { required: true, message: '请输入车架号', trigger: 'blur' },
        ],
        engineNum: [
          { required: true, message: '请输入发动机号', trigger: 'blur' },
        ],
        vehicleType: [
          { required: true, message: '请输入车辆类型', trigger: 'blur' },
        ],
        fuelType: [
          { required: true, message: '请输入燃油类型', trigger: 'blur' },
        ],
        seatNum: [
          { required: true, message: '请输入核定载人数', trigger: 'blur' },
        ],
        weight: [
          { required: true, message: '请输入整备质量', trigger: 'blur' },
        ],
        registerDate: [
          { required: true, message: '请选择初次注册日期', trigger: 'change'},
        ],
        txUnit: [
          { required: true, message: '请输入托修方', trigger: 'blur' },
        ],
        txUnitTel: [
          { required: true, message: '请输入托修方电话', trigger: 'blur' },
        ],
        whcycle: [
          { required: true, message: '请输入维护周期', trigger: 'blur' },
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    dilogClose(done) {
      this.$confirm('确认关闭？')
          .then(_ => {
              done();
          })
          .catch(_ => {});
    },
    handleClose(){
      this.fileList = [];
      this.openImportData = false;
    },
    handleRemove(file, fileList) {
    },
    handlePreview(file) {
    },
    handleExceed(files, fileList) {
      this.$message.warning(`只能上传一个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }？`);
    },
    uploadSuccess(response, file, fileList){
      if(response.code === 200){
        this.$message.success(response.msg);
        this.openImportData = false;
        this.fileList = fileList;
        this.getList();
      }else {
        this.$message.error(response.msg);
      }
    },
    openImport(){
      this.openImportData = true;
      this.fileList = [];
    },
    /** 查询汽车检测-车辆信息列表 */
    getList() {
      this.loading = true;
      listVehicleinfo(this.queryParams).then(response => {
        this.vehicleinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        licenseNum: null,
        brand: null,
        brandColor: null,
        fuelType: null,
        vehicleType: null,
        vin: null,
        engineNum: null,
        seatNum: null,
        weight: null,
        registerDate: null,
        txUnit: null,
        txUnitTel: null,
        whcycle: null,
        createTime: null,
        model: null,
        operationalNature: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加汽车检测-车辆信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVehicleinfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改汽车检测-车辆信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVehicleinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log(this.form);
            addVehicleinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除汽？').then(function() {
        return delVehicleinfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('vehicleservice/vehicleinfo/export', {
        ...this.queryParams
      }, `vehicleinfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
